#ifndef NETWORKPROTOCOL_H
#define NETWORKPROTOCOL_H

#include <QObject>

#include <QThread>

#include <QTcpSocket>
#include <QHostAddress>

#include <QQueue>

#include <QCoreApplication>

#include <QEventLoop>

#include "config.h"
#include "networkpacket.h"

class NetworkProtocol : public QObject
{
    Q_OBJECT
public:
    NetworkProtocol();
    void waitForReady();
    void sendPacket(NetworkPacket *packet);
    void doRun();

private:
    void processInQueue();
    void processOutQueue();
    QString repr(QByteArray rawData);
    QTcpSocket *client = 0;
    QQueue<NetworkPacket*> outQueue;

public slots:
//    void onConnect();
    void onSocketError(QAbstractSocket::SocketError error);
    void onSocketStateChanged(QAbstractSocket::SocketState);
    void readData();
signals:
    void onConnect();
    void onConnectionFailed();
    void onError();
    void onGotPacket(NetworkPacket *);
};

#endif // NETWORKPROTOCOL_H
